package com.tcm.order.service.task;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import com.base.genid.generator.UidGenerator;
import com.tcm.core.service.ConfigService;
import com.tcm.doctor.domain.dto.DoctorDTO;
import com.tcm.doctor.service.DoctorService;
import com.tcm.invite.domain.dto.InviteRecordDTO;
import com.tcm.invite.service.InviteRecordService;
import com.tcm.order.domain.dto.OrderBillTaskDTO;
import com.tcm.order.entity.OrderPlatformBill;
import com.tcm.order.dao.mapper.OrderPlatformBillMapper;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Service
public class OrderPlatformBillTask implements OrderBillTaskFactory {

    @Autowired
    private OrderPlatformBillMapper orderPlatformBillMapper;


    @DubboReference(version = "0.0.1-alpha", timeout = 6000, retries = 3, check = false)
    private InviteRecordService inviteRecordService;

    @DubboReference(version = "0.0.1-alpha", retries = 3, timeout = 6000)
    private ConfigService configService;

    @DubboReference(version = "0.0.1-beta", group = "cached-uid", timeout = 6000, check = false, retries = 3)
    private UidGenerator uidGenerator;

    @DubboReference(version = "0.0.1-alpha", timeout = 6000, retries = 3, check = false)
    private DoctorService doctorService;

    @Override
    public void create(OrderBillTaskDTO order) {
        DoctorDTO doctorDTO = doctorService.findDoctorByUserId(order.getDoctorId());
        if(ObjectUtil.isNotNull(doctorDTO)){
            OrderPlatformBill orderStoreBill = new OrderPlatformBill();

            orderStoreBill.setId(uidGenerator.getUID());
            orderStoreBill.setOrderId(order.getId());
            orderStoreBill.setOrderSn(order.getOrderSn());
            BigDecimal totalDrugsMoney = NumberUtil.mul(order.getDrugMoney(),order.getQuantity());
            Map doctorSpreadMoney = this.spreadMoney(totalDrugsMoney, doctorDTO.getMobile(), "doctor");
            orderStoreBill.setSecondMoney((BigDecimal) doctorSpreadMoney.get("spreadAmount"));
            orderStoreBill.setDoctor((String)doctorSpreadMoney.get("spreadPhone"));
            Map salesmanSpreadMoney = this.spreadMoney(totalDrugsMoney, doctorDTO.getMobile(), "salesman");
            orderStoreBill.setSpreadMoney((BigDecimal) salesmanSpreadMoney.get("spreadAmount"));
            orderStoreBill.setSalesman((String)salesmanSpreadMoney.get("spreadPhone"));
            orderStoreBill.setProfits(NumberUtil.mul(totalDrugsMoney,
                    new BigDecimal(configService.findValue("settlement_profits", "0.17"))));
            //计算诊金
            BigDecimal brokerage;
            BigDecimal billingCommission = new BigDecimal("0");
            BigDecimal platformBrokerage;
            switch (doctorDTO.getJobtitleLevel().intValue()){
                case 1 :
                    brokerage = new BigDecimal(configService.findValue("first_brokerage", "0.5"));
                    platformBrokerage = NumberUtil.sub(new BigDecimal("1"), brokerage);
                    billingCommission = NumberUtil.mul(platformBrokerage, order.getBillingMoney());
                    break;
                case 2 :
                    brokerage = new BigDecimal(configService.findValue("second_brokerage", "0.7"));
                    platformBrokerage = NumberUtil.sub(new BigDecimal("1"), brokerage);
                    billingCommission = NumberUtil.mul(platformBrokerage, order.getBillingMoney());
                    break;
                case 3 :
                    brokerage = new BigDecimal(configService.findValue("second_brokerage", "0.8"));
                    platformBrokerage = NumberUtil.sub(new BigDecimal("1"), brokerage);
                    billingCommission = NumberUtil.mul(platformBrokerage, order.getBillingMoney());
                    break;
            }
            //获取推广人
            //查询业务员
            InviteRecordDTO doctor = inviteRecordService.findByInvitedCode(doctorDTO.getMobile(),"salesman");
            if (ObjectUtil.isNotNull(doctor)){
                orderStoreBill.setSalesman(doctor.getCellphone());
            }
            InviteRecordDTO doctorRecord = inviteRecordService.findByInvitedCode(doctorDTO.getMobile(),"doctor");
            if (ObjectUtil.isNotNull(doctorRecord)){
                orderStoreBill.setDoctor(doctorRecord.getCellphone());
            }
            orderStoreBill.setBillingMoney(billingCommission);
            BigDecimal totalMoney = NumberUtil.add(
                    orderStoreBill.getProfits(),
                    orderStoreBill.getSpreadMoney(),
                    orderStoreBill.getSecondMoney(),
                    orderStoreBill.getBillingMoney());
            orderStoreBill.setTotalMoney(totalMoney);
            orderStoreBill.setCreateTime(DateUtil.date());
            orderStoreBill.setUpdateTime(DateUtil.date());
            orderStoreBill.setStatus("0");
            orderPlatformBillMapper.insert(orderStoreBill);
        }

    }

    /**
     * 推广提成
     */
    private Map spreadMoney(BigDecimal drugsMoney, String doctor, String spreadType){
        //查询推广员
        InviteRecordDTO inviteRecordDTO = inviteRecordService.findByInvitedCode(doctor, spreadType);
        BigDecimal spreadAmount = new BigDecimal("0");
        String spreadIdPhone = "";
        Map result = new HashMap();
        if(ObjectUtil.isEmpty(inviteRecordDTO)){
            result.put("spreadIdPhone", "");
            result.put("spreadAmount", new BigDecimal("0"));
            return result;
        }
        if(spreadType.equals("doctor")){
            //获取推荐医生的推广人
            spreadIdPhone = inviteRecordDTO.getCellphone();
            //奖励推荐医生
            BigDecimal settlement = new BigDecimal(configService.findValue("second_distribution", "0.05"));
            result.put("spreadPhone", spreadIdPhone);
            result.put("spreadAmount", NumberUtil.mul(settlement, drugsMoney));
        }else if(spreadType.equals("salesman")){
            spreadIdPhone = inviteRecordDTO.getCellphone();
            //业务员结算比例
            BigDecimal settlement = new BigDecimal(configService.findValue("settlement_salesman", "0.08"));
            result.put("spreadIdPhone", spreadIdPhone);
            result.put("spreadAmount", NumberUtil.mul(settlement, drugsMoney));
        }
        return result;
    }

}
